<?php 
class Node_model_mapping extends Model {
    private $table_keyword = 'keyword';
    private $table_node_keyword = 'node_keyword';
    private $table_node_mapping = 'node_mapping';
    
    public function add_keywords($keywords, $node_id = 0) {
        if ( ! is_array($keywords)) {
            tos_die('$keywords must be array');
        }
        $node_id = (int) $node_id;
        
        foreach ($keywords as $word) {
            $keyword_id = $this->_add_keyword($word);
            if ($keyword_id && $node_id) {
                $this->_add_node_keyword($keyword_id, $node_id);
            }
        }
    }
    
    
    public function add_mapping($prev_id, $keyword, $next_id) {
        $keywords = array($keyword);
        $this->add_keywords($keywords, $prev_id, 'next');
        $this->add_keywords($keywords, $next_id, 'prev');
        
    }
    
    private function _add_node_keyword($keyword_id, $node_id, $hit_type = '') {
        $keyword_id = (int) $keyword_id;
        $node_id = (int) $node_id;
        if ($node_keyword_id = $this->_node_keyword_exists($keyword_id, $node_id)) {
            $set_str = "`hit`=`hit`+1";
            if ($hit_type == 'prev' || $hit_type == 'next') {
                $set_str .= ",`{$hit_type}_hit`=`{$hit_type}_hit`+1";
            } 
            $sql = "UPDATE `{$this->table_node_keyword}` SET {$set_str} WHERE `id`='{$node_keyword_id}'";
            if ($this->db->execute($sql)) {
                return true;
            }
        } else {
            $data = array('keyword_id' => $keyword_id, 'node_id' => $node_id);
            if ($hit_type == 'prev' || $hit_type == 'next') {
                $data["{$hit_type}_hit"] = 1;
            } 
            if ($this->db->insert($this->table_node_keyword, $data)) {
                return true;
            }
        }
    }
    private function _node_keyword_exists($keyword_id, $node_id) {
        $sql = "SELECT `id` FROM `{$this->table_node_keyword}` WHERE `keyword_id`='{$keyword_id}' AND `node_id`='{$node_id}' LIMIT 1";
        if ($row = $this->db->row($sql)) {
            return $row['id'];
        } else {
            return false;
        }
    }
    
    private function _add_keyword($word) {
        $word = trim($word);
        if (empty($word)) {
            //tos_die('empty keyword');
            return false;
        }
        $word = $this->db->escape_str($word);
        
        if ($keyword_id = $this->_word_exists($word)) {
            $sql = "UPDATE `{$this->table_keyword}` SET `hit`=`hit`+1 WHERE `id`='{$keyword_id}'";
            if ($this->db->execute($sql)) {
                return $keyword_id;
            }
        } else {
            return $this->db->insert($this->table_keyword, array('word' => $word));
        }
        return false;
    }
    
    private function _word_exists($word) {
        $sql = "SELECT `id` FROM `{$this->table_keyword}` WHERE `word`='{$word}' LIMIT 1";
        if ($row = $this->db->row($sql)) {
            return $row['id'];
        } else {
            return false;
        }
    }
}
?>